<?php

/**
 * User: Titan
 * Date: Sep 28, 2010
 * Time: 10:07:39 PM
 */

/**
 * render()
 * 
 * An excellent function that finds the best way to render an object 
 * (page function, system function, or, in the case of render('page') calls, a template file)
 * 
 * This function takes a variable number of arguments.
 * 
 * The first argument is always the name of the object you are rendering--for example, 'page'.
 * 
 * The remaining arguments are anything that should be passed to the rendering function or template
 * 
 * @return the rendered HTML content
 */
/*
function render()
{
	$args = func_get_args();
	$model = array_shift($args);
	
	static $models = array();
	
	$output = '';
	
	// check to see if a rendering method has been used for this specific model
	// ie. for form elements. render('submit') is called a lot so it's unnecessary to search for it again
	if (isset($models[$model]))
	{
		$details = $models[$model];
		if (isset($details['template']))
		{
			// rendering the page
			$args[$model] = $args[0];
			
			// call the template's prerendering functions
			render_call_prerender($args, $model);
			
			// make sure the file exists before rendering it
			if (file_exists('./' . $args['template']))
			{
				$output = render_template($args['template'], $args);
			}
		}
		else if (isset($details['function']))
		{
			// use a rendering function, and make sure it exists
			if (function_exists($details['function']))
			{
				$output = call_user_func_array($details['function'], $args);
			}
		}
	}
	else
	{
		// use the loaded page's rendering functions
		// this is useful because different pages will have different rendering functions
		// ie. a page that lists developers will want different content than a
		// page that lists milestones
		global $load;
		
		$db = & get_db();
		if ($db->is_active())
		{
			$model_object = $db->fetch_object($db->query("SELECT model, type, prerender, file, template FROM render WHERE model='%s'", $model));
			
			render_call_prerender($args, $model, $model_object);
			if ($model_object->type == 'function')
			{
			
			}
		}
		
		// old way: 
		if (function_exists($load . '_' . $model))
		{
			$output = call_user_func_array($load . '_' . $model, $args);
			$models[$model]['function'] = $load . '_' . $model;
		}
		else if (file_exists('./templates/' . $model . '.tpl.php'))
		{
			$args[$model] = './templates/' . $model . '.tpl.php';
			
			render_call_prerender($args, $model);
			$output = render_template('./templates/' . $model . '.tpl.php', $args);
			$models[$model]['template'] = './templates/' . $model . '.tpl.php';
		}
		else if (function_exists('render_' . $model))
		{
			$output = call_user_func_array('render_' . $model, $args);
			$models[$model]['function'] = 'render_' . $model;
		}
	}
	
	return $output;
}
*/

/**
 * _render_build_models()
 *
 * @param mixed $models
 * @param stromg $name
 * @param int $type
 * @param string $path
 */
/*
function _render_build_models(&$models, $name, $type, $theme, $path)
{
	$result = array();
	$function = $name . '_render';
	if (function_exists($function))
	{
		$result = $function($models, $type, $path);
		
		foreach ($result as $model => $details)
		{
			$result[$model]['type'] = $type;
			$result[$model]['path'] = $path;
			
			if (!isset($details['template']) && !isset($details['prerender']))
			{
				$result[$model]['prerender'] = ($type == RENDER_TYPE_MODULE ? 'theme_' : $name . '_') . $model;
			}
			
			// TODO: drupal uses include files here. necessary?
			

			if (isset($details['template']) && !isset($details['path']))
			{
				$result[$model]['template'] = $path . '/' . $details['template'];
			}
			
			if (!isset($details['prerender']) || !is_array($details['prerender']))
			{
				$details['prerender'] = array();
				$prefixes = array();
				
				if ($type == RENDER_TYPE_MODULE)
				{
					$prefixes[] = 'model';
					
					$prefixes += module_list();
				}
				else
				{
					$prefixes[] = $name;
				}
			}
		}
	}
}
*/

/*
function render_build_models()
{
	$models = array();
	
	foreach (module_implements('render') as $module)
	{
		_render_build_models($models, $module, RENDER_TYPE_MODULE, $module, system_get_path('module', $module));
	}
}
*/

/**
 * Theme a heading (ie. <h1> element).
 * 
 * @param $text: the text to be included in the heading. Not translated here.
 * @param $level: the level of the heading (1 - 6).
 * @param $attributes: Optional attributes.
 * @return themed heading.
 */
/*
function render_heading($text, $level = 2, $attributes = array())
{
	if ($level > 6 || $level < 1)
	{
		system_set_message('Invalid heading level requested ' . $level, 'error');
		$level = 6;
	}
	
	return "<h$level " . attributes($attributes) . '>' . $text . "</h$level>";
}
*/

/**
 * render_attributes()
 * 
 * This is a function that will take in a key => value array 
 * and produce a string of HTML attributes.
 * 
 * ie. an array like array('class' => 'navigation') will return ' class="navigation"'
 * 
 * @param mixed $attributes
 * @return
 */
/*
function render_attributes($attributes = array())
{
	if (is_array($attributes))
	{
		$output = '';
		foreach ($attributes as $key => $value)
		{
			$output .= " $key=" . '"' . html_safe($value) . '"';
		}
		
		return $output;
	}
}
*/

/**
 * render_template()
 * 
 * This function gets the HTML content of a template file (ie. page.tpl.php)
 * 
 * @param mixed $file
 * @param mixed $vars
 * @return
 */
/*
function render_template($file, $vars = array())
{
	extract($vars);
	
	ob_start();
	include ('./' . $file);
	
	return ob_get_clean();
}
*/

/**
 * render_call_prerender()
 * 
 * This function calls any prerender functions that are defined by the system or the loading page 
 * 
 * @param mixed $variables
 * @param mixed $model
 * @return void
 */
/*
function render_call_prerender(&$variables, $model, $renderable = NULL)
{
	global $load;
	
	$parameters = array(
		&$variables, $model
	);
	
	if (!empty($renderable))
	{
		if ($renderable->type == 'template')
		{
			if (isset($variables[0]))
			{
				$variables = array_merge($variables[0], $variables);
				unset($variables[0]);
			}
		}
		
		system_call_function('model_prerender', $parameters);
		foreach ($renderable->prerender as $key => $prerender)
		{
			system_call_function($prerender, $parameters);
		}
		
		//	$sys_function = 'system_prerender_' . $function;
		//	if (function_exists($sys_function))
		//	{
		//		call_user_func_array($sys_function, $parameters);
		//	}
		

		system_call_function($load . '_prerender_' . $model, $parameters);
		
		// TODO: there's gotta be a better way
		system_call_function('system_prerender_cleanup_' . $model, $parameters);
	}
	else
	{
		system_call_function('model_prerender', $parameters);
		
		system_call_function('system_prerender_' . $model, $parameters);
		
		system_call_function($load . '_prerender_' . $model, $parameters);
	}
}
*/

/**
 * render_generic_button()
 * 
 * This function renders a <button> element
 * 
 * @param mixed $button
 * @return
 */
function render_generic_button($button)
{
	$output = '<button id="' . $button['#id'] . '"' . (isset($button['#title']) ? ' title="' . $button['#title'] . '"' : '') . ' type="button"' . (isset($button['#disabled']) && $button['#disabled'] ? ' disabled="disabled"' : '') . (empty($button['#attributes']) ? '' : attributes($button['#attributes'])) . '>' . check_plain($button['#title']) . '</button>' . "\n";
	$prefix = (isset($button['#prefix']) ? $button['#prefix'] : '');
	$suffix = (isset($button['#suffix']) ? $button['#suffix'] : '');
	
	return $prefix . $output . $suffix;
}

/**
 * render_system_messages()
 * 
 * @param mixed $messages
 * @return
 */
/*
function render_system_messages($messages)
{
	$output = '';
	foreach ($messages as $type => $message_list)
	{
		if (!empty($message_list))
		{
			$output .= '<div class="' . $type . '"><ul>' . "\n";
			
			for($i = 0; $i < count($message_list); $i++)
			{
				$output .= '<li class="message">' . $messages[$type][$i] . '</li>' . "\n";
			}
			
			$output .= '</ul></div>' . "\n";
		}
	}
	
	return $output;
}
*/

function render_table($table)
{
	$output = '<table' . (empty($table['#attributes']) ? '' : attributes($table['#attributes'])) . '>';
	
	if (isset($table['#header']) && count($table['#header']) > 0)
	{
		$output .= '<thead><tr>';
		
		foreach ($table['#header'] as $cell)
		{
			$output .= render_table_cell($cell, NULL, TRUE);
		}
		
		$output .= '</tr></thead>';
	}
	
	if (isset($table['#rows']) && count($table['#rows']) > 0)
	{
		$output .= '<tbody>';
		
		$row_count = count($table['#rows']);
		foreach ($table['#rows'] as $index => $row)
		{
			$classes = array(
				(($index + 1) % 2 == 0 ? 'even' : 'odd')
			);
			if ($index == 0)
			{
				$classes[] = 'first';
			}
			else if ($index == $row_count - 1)
			{
				$classes[] = 'last';
			}
			
			$output .= '<tr class="' . implode(' ', $classes) . '"' . (empty($row['#attributes']) ? '' : attributes($row['#attributes'])) . '>';
			
			if (isset($row['#cells']))
			{
				foreach ($row['#cells'] as $key => $cell)
				{
					$output .= render_table_cell($cell, $key);
				}
			}
			
			$output .= '</tr>';
		}
		
		$output .= '</tbody>';
	}
	
	$output .= '</table>' . "\n";
	
	return $output;
}

/**
 * render_table_cell()
 * 
 * @param mixed $cell
 * @param bool $header
 * @return
 */
function render_table_cell($cell, $key = NULL, $header = FALSE)
{
	$tag = 'td';
	if ($header)
	{
		$tag = 'th';
	}
	
	if (is_array($cell))
	{
		if (!empty($key))
		{
			if (empty($cell['#attributes']))
			{
				$cell['#attributes'] = array(
					'class' => $key
				);
			}
			else
			{
				if (empty($cell['#attributes']['class']))
				{
					$cell['#attributes']['class'] = $key;
				}
				else
				{
					$cell['#attributes']['class'] .= ' ' . $key;
				}
			}
		}
		
		return '<' . $tag . (empty($cell['#attributes']) ? '' : attributes($cell['#attributes'])) . '>' . $cell['#value'] . '</' . $tag . '>' . "\n";
	}
	else
	{
		return '<' . $tag . (empty($key) ? '' : ' class="' . html_class($key) . '"') . '>' . $cell . '</' . $tag . '>' . "\n";
	}
}

/*
function render_breadcrumbs($breadcrumbs)
{
	$output = '';
	if (!empty($breadcrumbs))
	{
		$separator = '</li><li class="breadcrumb"> > </li><li class="breadcrumb">';
		
		$output = '<div class="breadcrumbs"><ul><li class="breadcrumb first">' . implode($separator, $breadcrumbs) . '</li><li class="breadcrumb"> > </li><li class="breadcrumb last">' . system_page_title() . '</li></ul></div>';
	}
	
	return $output;
}
*/

function model_prerender(&$vars)
{
	global $user;
	
	$db = & get_db();
	if ($db->is_active())
	{
		$vars['logged_in'] = ($user->uid == 0 ? FALSE : TRUE);
		$vars['admin'] = ($user->uid == 1 ? TRUE : FALSE);
		$vars['user'] = $user;
	}
}
















